﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using System.IO;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;
using ZLCMS.Commands.User.Util;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Member
{
    public partial class MemberList : System.Web.UI.Page
    {

        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;
        public string _userAccount;
        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            
            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;
            _userAccount=UserKit.GetUserAccountFailLogin();

            string sql = " from MemberInfo where Status='1' ";

            QueryFilterList filters = new QueryFilterList();
            if (!string.IsNullOrEmpty(Request["account"]))
            {
                filters.Add(new StringFilter("AccountId", "%" + Request["account"] + "%", StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(Request["name"]))
            {
                filters.Add(new StringFilter("MemberName", "%" + Request["name"] + "%", StringCompairType.Like));
            }

            string departmentCode = "";
            if (AuthUtil.CheckAuthorization("DEPT_MAN", "ALL",_userAccount))
            {
            }
            else if (AuthUtil.CheckAuthorization("DEPT_MAN", "VIEW_OWNER", _userAccount))
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    departmentCode = session.CreateSQLQuery("select dept_code from sys_user_info where user_account=:acc")
                        .SetParameter("acc", _userAccount).UniqueResult<string>();
                }
            }
            else
            {
                //没有权限访问
                departmentCode = "no_dept";
            }
            if (!string.IsNullOrEmpty(departmentCode))
            {
                filters.Add(new StringFilter("DeptCode", departmentCode, StringCompairType.Equal));
            }

            foreach (DataFilter filter in filters)
            {
                sql += " and " + filter.GetSection();
            }

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                //判断是否导出EXCEL操作
                if (!string.IsNullOrEmpty(Request["exportexcel"]))
                {
                    IQuery query = session.CreateQuery(sql + " order by AddDate desc");
                       //.SetParameter("acc", UserKit.GetUserAccountFailLogin());

                    filters.SetUp(query);

                    IList<MemberInfo> list = query.List<MemberInfo>();
                   

                    StringWriter sw = new StringWriter();
                    sw.WriteLine("帐号\t姓名\t邮箱\t手机\t积分\t等级\t备注");
                    foreach (MemberInfo item in list)
                    {
                        sw.WriteLine("'" + item.AccountId + "\t" + item.MemberName + "\t" + item.Email + "\t'"
                             + item.Phone + "\t" + item.Score + "\t" + item.Level + "\t" + item.Remarks + "\t");
                    }
                    sw.Close();

                    OutputKit.OutputExcel(Response, sw);
                }
                else
                {

                    IQuery query = session.CreateQuery("select count(*)" + sql);
                        //.SetParameter("acc", UserKit.GetUserAccountFailLogin());
                    filters.SetUp(query);

                    //设置记录总数
                    _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());

                    query = session.CreateQuery(sql + " order by AddDate desc")
                        //.SetParameter("acc", UserKit.GetUserAccountFailLogin())
                        .SetFirstResult((pageIndex - 1) * pageSize)
                        .SetMaxResults(pageSize);
                    filters.SetUp(query);

                    //设置数据源
                    repeater1.DataSource = query.List<MemberInfo>();


                }
            }

            repeater1.DataBind();
        }
        

    }
}